<template>
  <div>
    <!-- 条件查询洗车扣款表单 -->
    <div class="check-form-box">
      <el-form
        :inline="true"
        :model="searchModel.data"
        class="demo-form-inline"
      >
        <el-form-item label="订单日期">
          <el-date-picker
            v-model="searchModel.createDate"
            type="daterange"
            range-separator="-"
            start-placeholder="开始时间"
            end-placeholder="结束时间"
          />
        </el-form-item>
        <el-form-item label="用户ID">
          <el-input
            v-model.trim="searchModel.userId"
            :formatter="(value) => value.replace(/[^\d]/g, '')"
            placeholder="请输入用户ID"
          />
        </el-form-item>
        <el-form-item label="订单状态">
          <el-select
            v-model="searchModel.compositeState"
            class="m-2"
            placeholder="请选择状态"
          >
            <el-option
              v-for="item in stateOptions"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="用户姓名">
          <el-input
            v-model.trim="searchModel.userName"
            placeholder="请输入用户姓名"
          />
        </el-form-item>
        <el-form-item label="用户类型">
          <el-select v-model="searchModel.userType" placeholder="请选择">
            <el-option label="全部" :value="null" />
            <el-option label="车主" :value="1" />
            <el-option label="代理商" :value="2" />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" color="#ea5b24" @click="searchOrder()"
            >查询</el-button
          >
          <el-button
            type="primary"
            color="#ea5b24"
            @click="exportOrder()"
            v-if="pinia.isShow[103]"
            >导出Excel</el-button
          >
        </el-form-item>
      </el-form>
    </div>

    <!-- 洗车扣款订单列表 -->
    <div style="margin-top: 20px">
      <el-table :data="tableData.data" style="width: 100%">
        <el-table-column prop="createTime" width="160" label="订单日期" />
        <el-table-column prop="orderNo" width="200" label="业务订单号" />
        <el-table-column prop="outNo" width="200" label="外部订单号" />
        <el-table-column prop="userId" width="120" label="用户ID" />
        <el-table-column prop="userName" label="用户姓名" />
        <el-table-column label="用户类型">
          <template v-slot:default="scope">
            {{
              scope.row.userType == null
                ? ""
                : scope.row.userType == 1
                ? "车主"
                : "代理商"
            }}
          </template>
        </el-table-column>
        <el-table-column prop="serviceItemName" width="100" label="消费场景" />
        <el-table-column prop="shopName" width="120" label="店铺名称" />
        <el-table-column prop="serviceName" width="120" label="消费子项" />
        <el-table-column prop="marketPrices" width="100" label="门市价" />
        <el-table-column
          prop="contractServicePrice"
          width="100"
          label="结算价"
        />
        <el-table-column prop="realPrice" width="110" label="实际支付金额" />
        <el-table-column
          width="100"
          :formatter="judgePayChannel"
          label="支付方式"
        />
        <el-table-column width="100" :formatter="judgeState" label="订单状态" />
      </el-table>
      <!-- 分页插件 -->
      <div style="text-align: center">
        <el-pagination
          :total="totalNum"
          @current-change="handleCurrentChange"
          background
          :page-size="size"
          layout="prev, pager, next, total"
          :current-page="page"
        />
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { onMounted, reactive, ref } from "vue";
import { ElMessage } from "element-plus";
import { findWashOrderApi, findWashExcelApi } from "../../../api/index";
import { formatDateTime } from "../../../utils/formatDateTime";
import { usePermissStore } from "../../../store/permiss";
const pinia = usePermissStore();

// 分页列表数据
let tableData: any = reactive([]);
const totalNum = ref(0);
const page = ref(1);
const size = 8;

// 翻页
let flipData = reactive({
  page: page,
  size: size,
});
const handleCurrentChange = (val: number) => {
  page.value = val;
  flipData.page = page.value;
  findOrder(flipData);
};

// 搜索模型
const searchModel: any = reactive({
  userId: null,
  userName: null,
  userType: null,
  compositeState: null,
  createDate: null,
  startTime: null,
  endTime: null,
  page: page,
  size: size,
});
// orderState	        订单状态         0:未支付 1:已支付 2:订单取消 3:订单过期
// latestRefundState	最后一次退款状态 0:未退款 1:退款中 2:退款失败 3:退款成功
// codeType	            二维码类型       0:未核销 1:已核销 2:已过期 null:未支付
// sdOrderStatus        洗车下单状态     0:未下单 1:下单成功 2:下单失败

// 过滤订单状态
function judgeState(item) {
  switch (item.compositeState) {
    case 1:
      return "未支付";
    case 2:
      return "已支付";
    case 4:
      return "退款中";
    case 5:
      return "退款成功";
    case 6:
      return "退款失败";
    case 7:
      return "支付中";
    case 8:
      return "支付失败";
    case 9:
      return "已取消";
    default:
      return "";
  }
}

// 过滤支付通道
function judgePayChannel(row) {
  let str = "";
  pinia.allDic?.order_summary_pay_channel?.dicList.forEach((item) => {
    if (item.dicValue == row.payChannel) {
      str = item.dicName;
      return;
    }
  });
  return str;
  // switch (item.payChannel) {
  //   case 1:
  //     return "易宝银行卡";
  //   case 2:
  //     return "富友银行卡";
  //   case 3:
  //     return "富友微信";
  //   case 4:
  //     return "富友支付宝";
  //   case 5:
  //     return "银联银行卡";
  //   case 6:
  //     return "银联微信";
  //   case 7:
  //     return "银联支付宝";
  //   case 8:
  //     return "新生银行卡";
  //   case 9:
  //     return "新生微信";
  //   case 10:
  //     return "新生支付宝";
  //   case 11:
  //     return "货通宝微信直连";
  //   case 12:
  //     return "富友9418-微信";
  //   case 13:
  //     return "富有支付宝二维码";
  //   case 14:
  //     return "新生支付宝二维码";
  //   default:
  //     return "";
  // }
}

// 状态枚举
const stateOptions: any = reactive([
  { value: null, label: "全部" },
  { value: "1", label: "未支付" },
  { value: "7", label: "支付中" },
  { value: "2", label: "已支付" },
  { value: "8", label: "支付失败" },
  { value: "4", label: "退款中" },
  { value: "5", label: "退款成功" },
  { value: "6", label: "退款失败" },
  { value: "9", label: "已取消" },
]);

// 搜索退款详情
const searchOrder = () => {
  page.value = 1;
  if (searchModel.createDate) {
    searchModel.startTime = formatDateTime(searchModel.createDate[0]);
    searchModel.endTime = formatDateTime(searchModel.createDate[1]).replace(
      "00:00:00",
      "23:59:59"
    );
  } else {
    searchModel.startTime = null;
    searchModel.endTime = null;
  }
  searchParmSearch();
  findOrder(searchModel);
  flipData = JSON.parse(JSON.stringify(searchModel));
};
// 搜索参数检查
const searchParmSearch = () => {
  Object.keys(searchModel).forEach((key) => {
    if (!searchModel[key]) {
      searchModel[key] = null;
    }
  });
};

// 查询退款详情
const findOrder = (searchModel) => {
  findWashOrderApi(searchModel).then((res) => {
    if (res.data.code === 2000) {
      tableData.data = res.data.data.items;
      totalNum.value = res.data.data.total;
    } else {
      ElMessage.error("查询失败");
    }
  });
};

//导出订单
const exportOrder = () => {
  findWashExcelApi(searchModel).then((res) => {
    const blob = new Blob([res.data]);
    const fileName = "洗车扣款订单数据.xls";
    const elink = document.createElement("a"); // 创建a标签
    elink.download = fileName; // 为a标签添加download属性
    elink.style.display = "none";
    elink.href = URL.createObjectURL(blob);
    document.body.appendChild(elink);
    elink.click(); // 点击下载
    URL.revokeObjectURL(elink.href); // 释放URL 对象
    document.body.removeChild(elink); // 释放标签
  });
};

onMounted(() => {
  findOrder(searchModel);
});
</script>
<style scoped>
:deep(.demo-form-inline) {
  display: grid;
  justify-items: stretch;
  grid-template-columns: repeat(auto-fill, 25%);
}

:deep(.el-select) {
  width: 100%;
}

:deep(.el-form-item) {
  width: 90%;
}
</style>
